package com.mall.manager.service;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import com.mall.common.dao.MongoDBManager;
import com.mall.common.dao.MysqlDBManager;
import com.mall.untils.DateUtils;
import com.mongodb.DBObject;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

/**
 * @全球数量统计
 * @author liugb
 * @date 2016 7 14
 */
@Service
public class AllPeopleStatisticsService {
	@Resource
    private JdbcTemplate jdbcTemplate;
	//日志信息
	private Logger logger = LoggerFactory.getLogger(AllPeopleStatisticsService.class);	
	/**
	 * 获取圆所 数据
	 * @return
	 */
	public List<String> findAllSchool() {

		List<DBObject> findAll = MongoDBManager.findAll("php_school");
		List<String> stingList = new ArrayList<String>();
		String res3;
		try {
			for (DBObject dbObject : findAll) {
				String string = String.valueOf(dbObject.get("city"));
				String string2 = String.valueOf(dbObject.get("area"));
				String string3 = String.valueOf(dbObject.get("province"));
				res3 = "school,s" + string + ",s" + string2 + ",s" + string3;
				stingList.add(res3);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.info(e.getMessage());
		}

		return stingList;
	}
	
	
	
	/**
	 * 获取宝宝的数据
	 */
	public List<String> findAllBaby() {
		List<String> ldb = new ArrayList<String>();
		List<DBObject> findAll = MongoDBManager.findAll("php_baby");
		Map<String, Object> tj=null;
		String city = "";
		String area =  "";
		String province = "";
		String res ="";
		try {
			for (DBObject dbObject : findAll) {
				tj = new HashMap<String, Object>();
				tj.put("id", dbObject.get("school_id"));
				DBObject findOne = MongoDBManager.findOne("php_school", tj);
				if(findOne!=null && !findOne.equals("null")){
					city = String.valueOf(findOne.get("city"));
					area = String.valueOf(findOne.get("area"));
					province = String.valueOf(findOne.get("province"));
					res = "baby,b"+city+",b"+area+",b"+province;
					ldb.add(res);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.info(e.getMessage());
		}	
		return ldb;
	}
	
	/**
	 * 获取班级数据
	 * @return
	 */
	public List<String> findAllClass() {
		List<String> ldb = new ArrayList<String>();
		List<DBObject> findAll = MongoDBManager.findAll("php_class");
		Map<String, Object> tj=null;
		String city = "";
		String area =  "";
		String province = "";
		String res ="";
		try {
			for (DBObject dbObject : findAll) {
				tj = new HashMap<String, Object>();
				tj.put("id", dbObject.get("school_id"));
				DBObject findOne = MongoDBManager.findOne("php_school", tj);
				if(findOne!=null && !findOne.equals("null")){
					city = String.valueOf(findOne.get("city"));
					area = String.valueOf(findOne.get("area"));
					province = String.valueOf(findOne.get("province"));
					res = "class,c"+city+",c"+area+",c"+province;
					ldb.add(res);
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			logger.info(e.getMessage());
		}
		return ldb;
	}
	/**
	 * 圆所，教师，学生统计
	 * @param type
	 * @param sum
	 */
	public static void manager_insert_allpeople(int schoolnum,int teachernum,int studentnum){
		Connection conn = null;
		PreparedStatement ps = null;		    
		try {
				String sql="insert into db_processing.t_all_people_statistics(garden_sum,teacher_sum,student_sum,use_card,last_card,create_time,update_time) values(?,?,?,?,?,?,?)";
				conn = MysqlDBManager.getConnection();
				ps = (PreparedStatement) conn.prepareStatement(sql);
				ps.setInt(1, schoolnum);
				ps.setInt(2, teachernum);
				ps.setInt(3, studentnum);
				ps.setInt(4, 0);
				ps.setInt(5, 0);
				ps.setTimestamp(6, DateUtils.getNowBeforTimestamp());
				ps.setTimestamp(7, DateUtils.getTimestamp());
				ps.addBatch();
				ps.executeBatch();
				conn.commit();			  
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}	
	/**
	 * 
	 * @param schoolnum
	 * @param teachernum
	 * @param studentnum
	 */
	public static void manager_update_allpeople(int schoolnum,int teachernum,int studentnum){
		Connection conn = null;
		PreparedStatement ps = null;
		try {
			String sql = "";
			if (schoolnum == 0) {
				sql = "update db_processing.t_all_people_statistics set teacher_sum = ? where teacher_sum = 0";
				conn = MysqlDBManager.getConnection();
				ps = (PreparedStatement) conn.prepareStatement(sql);
				ps.setInt(1, teachernum);
			} else if (teachernum == 0) {
				sql = "update db_processing.t_all_people_statistics set garden_sum = ? where garden_sum = 0";
				conn = MysqlDBManager.getConnection();
				ps = (PreparedStatement) conn.prepareStatement(sql);
				ps.setInt(1, schoolnum);
			}
			ps.executeUpdate();
			ps.executeBatch();
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}	
}
